﻿using HCMS.CommonDtos;
using HCMS.Entities.Manage;
using HCMS.IRepositories.Manage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;

namespace HCMS.Repositories.Manage
{
    /// <summary>
    /// 管理员 操作 日志 仓储层
    /// </summary>
    public class AdminActionLogsRepository : ManageBaseRepository<AdminActionLogs>, IAdminActionLogsRepository
    {
        public AdminActionLogsRepository(IDbContextProvider<ManageDbContext> contextProvider)
            : base(contextProvider)
        {
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<AdminActionLogs>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var query = await GetQueryableAsync();

            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                switch (searchArgs.SearchName)
                {
                    case "ids":
                        var ids = searchArgs.SearchKey.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (ids != null && ids.Length > 0)
                        {
                            var idList = Array.ConvertAll(ids, int.Parse);
                            query = query.Where(t => idList.Contains(t.AdminId));
                        }
                        else
                        {
                            query = query.Where(t => 1 == 2);
                        }
                        break;
                    case "url":
                        query = query.Where(t => t.Url.Contains(searchArgs.SearchKey));
                        break;
                    case "controllerName":
                        query = query.Where(t => t.ControllerName.Contains(searchArgs.SearchKey));
                        break;
                    case "actionName":
                        query = query.Where(t => t.ActionName.Contains(searchArgs.SearchKey));
                        break;
                    case "controllerNameList":
                        var controllerNames = searchArgs.SearchKey.Split(',', StringSplitOptions.RemoveEmptyEntries);
                        if (controllerNames != null && controllerNames.Length > 0)
                        {
                            query = query.Where(t => controllerNames.Contains(t.ControllerName));
                        }
                        else
                        {
                            query = query.Where(t => 1 == 2);
                        }
                        break;
                    case "actionNameList":
                        var actionNames = searchArgs.SearchKey.Split(',', StringSplitOptions.RemoveEmptyEntries);
                        if (actionNames != null && actionNames.Length > 0)
                        {
                            query = query.Where(t => actionNames.Contains(t.ActionName));
                        }
                        else
                        {
                            query = query.Where(t => 1 == 2);
                        }
                        break;
                }
            }

            var count = await query.CountAsync();
            pager.RowCount = count;

            var list = new List<AdminActionLogs>();

            if (count > 0)
            {
                query = query.OrderByDescending(t => t.Id);

                list = await query
                           .Skip((pager.CurrentPageIndex - 1) * pager.PageSize)
                           .Take(pager.PageSize)
                           .ToListAsync();
            }

            var result = new ListPagerDto<AdminActionLogs>()
            {
                Data = list ?? new List<AdminActionLogs>(),
                PagerInfo = pager
            };

            return result;
        }
    }
}